package com.mask.common.core.model;

import lombok.Data;

import java.io.Serializable;

/**
 * 通用返回对象
 */
@Data
public class R<T> implements Serializable{

    private static final long serialVersionUID = 19973609571968584L;

    private String code;
    private String msg;
    private T data;
    private String traceId;

    public static <T> R<T> ok() {
        return restResult(null, "200", "操作成功");
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, "200", "操作成功");
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, "200", msg);
    }

    public static <T> R<T> fail() {
        return restResult(null, "500", "操作失败");
    }

    public static <T> R<T> fail(String msg) {
        return restResult(null, "500", msg);
    }

    public static <T> R<T> fail(T data) {
        return restResult(data, "500", "操作失败");
    }

    public static <T> R<T> fail(T data, String msg) {
        return restResult(data, "500", msg);
    }

    public static <T> R<T> fail(String code, String msg) {
        return restResult(null, code, msg);
    }

    private static <T> R<T> restResult(T data, String code, String msg) {
        R<T> r = new R<>();
        r.setCode(code);
        r.setData(data);
        r.setMsg(msg);
        r.setTraceId("LogUtils.getTraceId()");
        return r;
    }
}